%%--------------------------------------------------------------------
%% Author: <vjache>
%% Created: 30.03.2009
%% Description: Iterator over all keys in an ets table.
%%--------------------------------------------------------------------
-module(iter_join_cprod_flattener).

%%--------------------------------------------------------------------
%% Include files
%%--------------------------------------------------------------------
-include("../include/iter.hrl").
%%--------------------------------------------------------------------
%% Exported Functions
%%--------------------------------------------------------------------
-export([
		 new/1,
		 open/1,
		 next/1,
		 close/1,
		 test/0
		 ]).

%%--------------------------------------------------------------------
%% API Functions
%%--------------------------------------------------------------------

new(IterCProd) ->
	iter:from_module(?MODULE, IterCProd).

open(IterCProd) ->
	_IterState=iter:open(IterCProd).

next(IterState) ->
	{IterStateNew,Item}=iter:next(IterState),
	case Item of
		?EOF->
			{IterStateNew,Item};
%% 		?REJECT_REC ->
%% 			throw({reject_rec,IterState});
		_ ->
			{IterStateNew,iter_join_cprod:row_flatten(Item)}
	end.
close(IterState) ->
	ok=iter:close(IterState).

%%--------------------------------------------------------------------
%% Test Functions
%%--------------------------------------------------------------------

test() ->
	I=new(iter:from_list([{'$cprod',{'$cprod',{k6,val6},{k6,val26}},{k6,val368}}])),
	iter:foreach(I,fun(E)->
						   io:format("~p~n",[E])
				 end),
	ok.

%%--------------------------------------------------------------------
%% Local Functions
%%--------------------------------------------------------------------